package homework;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Count {
	public int Money(String name) {
		FileReader fr = null;
		try {
			fr = new FileReader("net-log.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		BufferedReader br = new BufferedReader(fr);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = "";
		Date loginDate = new Date();
		Date logoutDate = new Date();
		try {
			while ((str = br.readLine()) != null) {
				String[] contends = str.split(",");
				if (contends[0].equals(name)) {
					if (contends[1].equals("up")) {
						loginDate = sdf.parse(contends[2]);
					} else if (contends[1].equals("down")) {
						logoutDate = sdf.parse(contends[2]);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		try {
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fr.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return (int) Math.ceil((logoutDate.getTime() - loginDate.getTime()) / 1000.0 / 1800);
	}
}
